home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 329_01 / csubst.l < prev    next >
Text File  |  1990-10-27  |  4KB  |  179 lines

  1. /* csubst.l */
  2. /* lex source of csubst utility.
  3. This programme is shareware.
  4. If you found this programme useful then send me a colourful postcard
  5. with the words "Happy Birthday" (or equivalent) so that it arrives
  6. at my address around the first of January 1991.
  7.  
  8. Henri de Feraudy
  9. 27 rue Chef de Ville
  10. 92140 Clamart 
  11. France
  12. */
  13. %{
  14. extern int  Mode, Truncation;
  15. extern char *lookup_subst();
  16. #include <stdio.h>
  17. #include "csubst.h"
  18. #define cond_putchar(C) if(Mode == APPLY_SUBSTS || Mode == PRINT_TRUNCATED)putchar(C);
  19.  
  20. #ifdef yywrap
  21. #undef yywrap
  22. #define yywrap() (!next_yyin())
  23. #endif
  24.  
  25. void action_keyword(), default_action(), action_symbol(), read_comment();
  26. %}
  27. letter         [a-zA-Z]
  28. rest        [a-zA-Z0-9_]
  29. identifier    {letter}{rest}*
  30. space        [ \t]*
  31. sinclude    "#"{space}"include"{space}"<"[a-zA-Z\.\\\/0-9]+">"
  32. hexa        [a-fA-F0-9]
  33. begin_define    "#"{space}"define"{space}
  34. begin_cond    "#"{space}("ifdef"|"ifndef")
  35.  
  36. %start        CPP_MACRO
  37. %%
  38.  
  39. "/*"            {read_comment();}
  40. {sinclude}        {default_action();}
  41. {begin_cond}        {default_action();BEGIN CPP_MACRO;}
  42. {begin_define}        {default_action();BEGIN CPP_MACRO;}
  43. "#"{space}{identifier}  {default_action();}
  44. "auto"            {action_keyword();}
  45. "break"            {action_keyword();}
  46. "case"            {action_keyword();}
  47. "char"            {action_keyword();}
  48. "const"            {action_keyword();}
  49. "continue"        {action_keyword();}
  50. "default"        {action_keyword();}
  51. "do"            {action_keyword();}
  52. "double"        {action_keyword();}
  53. "else"            {action_keyword();}
  54. "enum"            {action_keyword();}
  55. "extern"        {action_keyword();}
  56. "float"            {action_keyword();}
  57. "for"            {action_keyword();}
  58. "goto"            {action_keyword();}
  59. "if"            {action_keyword();}
  60. "int"            {action_keyword();}
  61. "long"            {action_keyword();}
  62. "register"        {action_keyword();}
  63. "return"        {action_keyword();}
  64. "short"            {action_keyword();}
  65. "signed"        {action_keyword();}
  66. "sizeof"        {action_keyword();}
  67. "static"        {action_keyword();}
  68. "struct"        {action_keyword();}
  69. "switch"        {action_keyword();}
  70. "typedef"        {action_keyword();}
  71. "union"            {action_keyword();}
  72. "unsigned"        {action_keyword();}
  73. "void"            {action_keyword();}
  74. "volatile"        {action_keyword();}
  75. "while"            {action_keyword();}
  76. \"(\\.|[^\\"])*\"    {action_symbol(TYPE_STRING);}
  77. '(\\.|[^\\'])+'        {default_action();}
  78. <CPP_MACRO>{identifier}    {action_symbol(TYPE_MACRO);BEGIN 0;}
  79. {identifier}        {action_symbol(TYPE_IDENTIFIER);BEGIN 0;}
  80. \n            {cond_putchar('\n');}
  81. "0x"{hexa}        {default_action();}
  82. .            {default_action();}
  83. %%
  84.  
  85. #ifndef yywrap /* flex defines this as a macro */
  86. /* this is the action yylex() executes on end of file 
  87.    if it returns 0 then processing continues.
  88.  */
  89. yywrap()
  90. {
  91. return (!next_yyin());
  92. }
  93. #endif
  94.  
  95. void action_symbol(type_flag)
  96. symbol_type_t type_flag;
  97. extern  char O_string_buffer[];
  98. static int reading_original_string = 1;
  99. char *o_string;
  100.  
  101.   switch(Mode)
  102.   {
  103.   case PRINT_TRUNCATED:
  104.     if(!symbol_lookup(yytext) && type_flag == TYPE_IDENTIFIER)
  105.         printf("%.*s", Truncation, yytext);
  106.     else
  107.       printf("%s", yytext);
  108.     break;
  109.  
  110.   case APPLY_SUBSTS:
  111.   o_string =  lookup_subst(yytext);
  112.   if(o_string == NULL)
  113.    printf("%s", yytext);
  114.   else
  115.    printf("%s", o_string);
  116.   break;
  117.  
  118.   case READ_IGNORES:
  119.   case EXTRACT_SYMBOLS:
  120.     symbol_install(yytext, type_flag);
  121.   break;
  122.  
  123.   case READ_SUBSTS:
  124.     if(reading_original_string)
  125.       {
  126.       strncpy(O_string_buffer,yytext,SYMBOL_SIZE);
  127.       }
  128.     else
  129.       {
  130.       install_subst(O_string_buffer,yytext);
  131.       }
  132.     reading_original_string = !reading_original_string;
  133.   }
  134.  
  135. }   
  136.  
  137.  
  138. void read_comment()
  139. {
  140. int c, previous;
  141.  
  142. previous = '*';
  143.  
  144. if(Mode == APPLY_SUBSTS || Mode == PRINT_TRUNCATED)
  145.    printf("/*");
  146.  
  147. for(;;){
  148.     c = input();
  149.     if(c == EOF)
  150.       fatal("EOF in comment");
  151.         else 
  152.     cond_putchar(c);
  153.     if(c == '/'){
  154.       if(previous == '*')
  155.         break;
  156.       }
  157.     previous = c;
  158.     }
  159.  
  160. }
  161.  
  162. void default_action()
  163. {
  164. if(Mode == APPLY_SUBSTS || Mode == PRINT_TRUNCATED)
  165.   printf("%s", yytext);
  166. }
  167.  
  168. void action_keyword()
  169. {
  170. if(Mode == READ_SUBSTS)
  171.   fatal1("Cannot use %s in a replacement", yytext);
  172. if(Mode == APPLY_SUBSTS || Mode == PRINT_TRUNCATED)
  173.    printf("%s", yytext);
  174. }
  175.  
  176. /* end of file */
  177.  
  178.